package com.baosight.sczx.zm.cm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cm.Level2MaterialBase;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBase;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMDepot;
import com.baosight.sczx.zm.cm.domain.ZMCMInventorydetail;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfo;

public class ServiceZMCM51 extends ServiceEPBase{
	public String fdInventoryId;
	
	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-27 19:58:11 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		return outInfo;
	}
	
	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 16:19:08 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		fdInventoryId=inInfo.getString("fdInventoryId");//主表id
		StaticTools.setQueryParameters(inInfo, "fdInventoryId", fdInventoryId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCMInventorydetail.query",new ZMCMInventorydetail());
		return outInfo;
	}
	
	/**新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-31下午02:35:57 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验物资分类是否重复
		boolean validate = validate(inInfo, "ZMCMInventorydetail.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else 
		{
			Map inMap = new HashMap();
			inMap.put("fdDepotId", inInfo.getBlock("result").getCell(0, "fdDepotId").toString());
			inMap.put("fdRowNum", "2");
			List _list = dao.query("ZMCMInventory.queryLastInvetoryDate", inMap);
			String lastDate = StaticTools.getYear()+"-01-01";
			if(_list.size()>0)
			{
				lastDate = ((Map)_list.get(0)).get("fdInventoryDate").toString();
			}
			
			inMap.put("fdLastDate", lastDate);
			_list = dao.query("ZMCMInventorydetail.queryImportAndExportNum", inMap);
			
			if(_list.size()>0)
			{
				inInfo.getBlock("result").setCell(0, "fdImportNum", ((Map)_list.get(0)).get("fdImportNum"));
				inInfo.getBlock("result").setCell(0, "fdExportNum", ((Map)_list.get(0)).get("fdExportNum"));
			}
			else
			{
				inInfo.getBlock("result").setCell(0, "fdImportNum", "0");
				inInfo.getBlock("result").setCell(0, "fdExportNum", "0");
			}
			
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_INVENTORYDETAIL"));//主键
			EiInfo outInfo = super.insert(inInfo, "ZMCMInventorydetail.insert");
			
			return outInfo;
		}
	}
	
	/**根据库房id，新增明细
	 * <p>Function: insertDetail</p>
	 * <p>Description:根据库房id，一次把该库房中的物资全部新增到明细表 </p>
	 * <p>param zbid:主表主键</p>
	 * <p>return value: EiInFo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-31下午02:35:57 李红星
	 * </p>
	 *
	 */
	public EiInfo insertDetail(EiInfo inInfo){
			inInfo.getBlock("result").setCell(0,"fdInventoryId",inInfo.getBlock("result").getCell(0, "fdKeyId"));
			EiInfo outInfo;
			if(inInfo.getBlock("result").getCell(0, "fdDepotCode").toString().equals("DEFAULT_DEPOT"))//默认的一级库房
			{
				outInfo = super.insert(inInfo, "ZMCMInventorydetail.batchinsert");
			}else
			{
				outInfo = super.insert(inInfo, "ZMCMInventorydetail.batch2Levelinsert");
			}
			
			return outInfo;
	}
	
	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23 16:21:08 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
        //校验物资分类是否重复
		//boolean validate = validate(inInfo, "ZMCMInventorydetail.validate", new HashMap<String, String>());
		
//		if(validate) {
//			return inInfo;
//		}
		EiInfo outInfo = super.update(inInfo, "ZMCMInventorydetail.update");
		return outInfo;
	}
	
	/**
	 *
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-22下午02:35:57 李红星
	 * </p>
	 *
	 */	
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
	   String fdMaterialInnerCode=inInfo.getBlock("result").getCellStr(0,"fdMaterialInnerCode");//物资内码
	   String fdKeyId=inInfo.getBlock("result").getCellStr(0,"fdKeyId");//主键
       params.put("fdMaterialInnerCode", fdMaterialInnerCode);
       params.put("fdInventoryId", fdInventoryId);
       params.put("fdKeyId", fdKeyId);
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("物资编码必须唯一");
			return true;
		}
		return false;
	}
	/**
	 * 明细customview 取物资信息
	 * <p>Function: materialcode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-27 18:43:37  李红星
	 * </p>
	 *
	 */
	public EiInfo materialcode(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.queryForMaterialcode",new ZMCMMaterialBasicInfo());
		return outInfo;
	}
	
	/**得到物资账面金额
	 * <p>Function getStockValue</p>
	 * @param inInfo
	 * @param string
	 * @param name
	 *  <p>History:
	 *    2011-6-27 20:13:04 李红星
	 * </p>
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo getStockValue(EiInfo inInfo) 
	{
		Map inMap = new HashMap();
		inMap.put("fdKeyId", inInfo.get("fdDepotId").toString());
		List list = dao.query("ZMCMDepot.query", inMap);
		boolean level1 = true;
		if(list.size() > 0)
		{
			ZMCMDepot depot = (ZMCMDepot)list.get(0);
			if(depot.getFdDepotLevel().equals("二级"))
			{
				inInfo.set("fdDepotCode", depot.getFdDepotCode());
				level1 = false;
			}
		}
		
		EiInfo outInfo = new EiInfo(); 
		if(level1)
		{
			outInfo = MaterialBase.getStockValue(inInfo); //根据物资内码得到库存数量
		}
		else
		{
			outInfo = Level2MaterialBase.getStockValue(inInfo);
		}
		
		return outInfo;
	}
	
	/**数据删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28 18:37:56  李红星  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo _temp = super.delete(inInfo, "ZMCMInventorydetail.delete","result");
		EiInfo outInfo = this.query(inInfo);		
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: queryNoteCode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:22:57 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo queryNoteCode(EiInfo inInfo)
	{
		List<Map> _list = new ArrayList<Map>();
		HashMap	params = new HashMap<String, String>();
		String startStr = "PD" + StaticTools.getDateDefault() + StaticTools.getTime().replaceAll(":", "");
		String defaultStr = startStr + "001";//定义格式
		_list = dao.query("ZMCMInventory.querySerialNum", params);
		if(_list!=null && _list.size() > 0) {
			String str = _list.get(0).get("fdNoteCode").toString();
			defaultStr = convertStr(startStr, str);
		}
		
		inInfo.set("fdNoteCode", defaultStr);
		
		return inInfo;
	}
	
	private String convertStr(String startStr, String str)
	{
		String str1 = str.substring(16,19);//第二部分的流水号
		int nub = Integer.parseInt(str1); 
		str1 = Integer.toString(nub+1);
		if(str1.length() == 1){
			return startStr+"00"+str1;
		}else if (str1.length() == 2)
		{
			return startStr+"0"+str1;
		}else
		{
			return startStr+str1;
		}
	}
}

